﻿namespace Patterns4Net.PatternEnforcer.Core.ClassCheckers
{
    using System;
    using System.Linq;
    using System.Linq.Expressions;
    using Mono.Cecil;
    using Mono.Cecil.Rocks;

    /// <summary>
    /// Checks methods of given class.
    /// Methods to be checker are chosen using predicate and
    /// their are check using <see cref="IChecker{MethodDefinition}"/>.
    /// </summary>
    public class TypeMethodsChecker : IChecker<TypeDefinition>
    {
        private readonly Func<MethodDefinition, bool> selector;

        private readonly IChecker<MethodDefinition> methodChecker;

        public TypeMethodsChecker(
            Func<MethodDefinition, bool> selector, 
            IChecker<MethodDefinition> methodChecker)
        {
            this.selector = selector;
            this.methodChecker = methodChecker;
        }

        public CheckerResult Check(TypeDefinition typeDefinition)
        {
            var results = (from method in typeDefinition.GetMethods()
                           where this.selector.Invoke(method)
                           select this.methodChecker.Check(method)).ToList();

            return new CheckerResult(results);
        }
    }
}
